--[[
夫妻副本逻辑
--]]
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local ObjHuman = require("core.ObjHuman")
local Timer = require("core.Timer")
local CopyDefine = require("copy.Define")
local CopyHandler = require("copy.HandlerL")
local Copy = require("copy.CopyL")
local Loss = require("copy.Loss")
local Aim = require("copy.Aim")


function applyCopyRoomCouple(copyID, config, human)
	if Copy.copyList[copyID] == nil then return end

	for roomID in pairs(Copy.copyList[copyID]) do
		if Copy.roomDatas[copyID][roomID] == nil then
			local team = TeamLogic.getTeamByUuid(human.db._id)
			Copy.roomDatas[copyID][roomID] = {member={}, onlineNum=0,startTime=Timer.now,matchType=CopyDefine.MATCH_COPY_DEFAULT}
			Copy.copyHuman2Room[human.db._id] = roomID
			Copy.copyTeam2Room[team.id] = roomID
			Copy.copyTeam2CopyID[team.id] = copyID
			Copy.roomBegin(copyID, roomID)
			return roomID
		end
	end
	return nil
end

function isEntered(human, copyID, roomID)
	local members = Copy.roomDatas[copyID][roomID].member
	for index, uuid in ipairs(members) do
		if uuid == human.db._id then
			return true
		end
	end
	
	return nil
end

function humanBeginCopy(human, copyID, roomID)
	local copyConfig = CopyConfig[copyID]

	local bEntered = isEntered(human, copyID, roomID)
	if bEntered == true then
		-- 已经进入过了
	else
		-- 还没有进入过
		CopyHandler.incCopyEnterCount(human, copyID)
		human.db.copyExit = nil
		
		Loss.handler(human, copyConfig, copyID)	-- 扣除进入需要的消耗 
		local members = Copy.roomDatas[copyID][roomID].member
		members[#members + 1] = human.db._id
	end
	
	Copy.copyHuman2Room[human.db._id] = roomID

	-- 根据副本目标进度获取进入的地图号
	local mapID, x, y = Aim.getEnterMap(copyID, roomID)
	if mapID == nil then
		mapID = copyConfig.mapList[1]
		local sceneConfig = ExcelMap.scene[mapID]
		x = sceneConfig.x
		y = sceneConfig.y
	end
	ObjHuman.enterMap(human, mapID, x, y)
end


function humanEndCopy(human)
	local sceneID = human.scene_id
	local copyID = Copy.sceneID2CopyID[sceneID]
	--local roomID = Copy.sceneID2Room[sceneID]
	
	local curCnt = human.db.copyExit or 0
	human.db.copyExit = curCnt + 1
	
	local backHomeMapId, backHomeX, backHomeY = Copy.getBackHomePoint(human, copyID)
	ObjHuman.enterMap(human,backHomeMapId,backHomeX,backHomeY)
	
	local team = TeamLogic.getTeamByUuid(human.db._id)
	if team and team.count == 1 then
		TeamLogic.exit(human.db._id)
	end
end
